/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StudentFeesDetail;

import SubFeeHead.SubFeeHeadBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aamod
 */
public class FeeDetail extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            StringBuilder stb=new StringBuilder();
            int amount=0,flag=0,totalamount=0;
            int id=Integer.parseInt(request.getParameter("feeid"));
            int admid=Integer.parseInt(request.getParameter("admid"));
            int catid=Integer.parseInt(request.getParameter("catid"));
            int semester=Integer.parseInt(request.getParameter("semester"));
            int coid=Integer.parseInt(request.getParameter("coid"));
            String sess=request.getParameter("sess");
            String duration="";
            float fdeposited=StudentFeesDetail.Query.totalFees(admid+"-"+semester+"-"+sess+"-"+id);
            Vector<Object> list1=StudentFeesDetail.Query.veiwAll("--"+id+"--"+admid+"--"+semester+"--"+catid);
            if(SubFeeHead.Query.viewAll(id).size()>0)
            {
            if(id>0)
            {
                out.println("<legend>");
                out.println("Fees Detail");
                out.println("</legend>");
                out.println("<table class='table table-striped dataTable-noheader dataTable-nofooter table-bordered' style='float:left;'><thead>");
                out.println("<th></th>");
                out.println("<th>Sub Fee Type</th>");
                out.println("<th>Duration</th>");
                out.println("<th>Amount</th>");
                out.println("<th>Additional Amount</th>");
                out.println("<th>Rebate</th>");
                out.println("<th>Total Amount</th>");
                out.println("</thead><tbody>");    
                if(list1.isEmpty())
                {
                Vector<Object> list=SubFeeHead.Query.viewAll(id);
                if(!list.isEmpty())
                {
                Iterator it=list.iterator();
                while(it.hasNext())
                {
                SubFeeHeadBean sbh=(SubFeeHeadBean)it.next();
                stb.append(sbh.getSubFeeTypeId()+"/");
                amount=Fees.Query.getAmount(sbh.getSubFeeTypeId()+"/"+coid+"/"+semester+"/"+catid+"/"+sess);
                if(sbh.getDuration()==1)
                {
                   duration="Once in a Session"; 
                }
                else if(sbh.getDuration()==2)
                {
                    duration ="Annually";
                }
                else if(sbh.getDuration()==3)
                {
                    duration="Half Yearly";
                }
                else if(sbh.getDuration()==4)
                {
                    duration="Quaterly";
                }
                else
                {
                    duration="Monthly";
                }
                out.println("<tr><td><center><input type='checkbox' name='"+sbh.getSubFeeTypeId()+"-c' id='"+sbh.getSubFeeTypeId()+"-c' onclick='calculateFees(this.id)'></center></td><td>"+sbh.getSubFeeType()+"</td><td style='width:100px'>"+duration+"</td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-a' id='"+sbh.getSubFeeTypeId()+"-a' readonly style='width:150px' value='"+amount+"'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-add' id='"+sbh.getSubFeeTypeId()+"-add' style='width:150px' value='0' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-r' id='"+sbh.getSubFeeTypeId()+"-r' value='0' style='width:150px' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-p' id='"+sbh.getSubFeeTypeId()+"-p' value='0' style='width:150px' readonly></center></td>");
                out.println("</tr>");
                }
                }
               }
                else
                {
                Vector<Object> list=SubFeeHead.Query.viewAll(id);
                if(!list.isEmpty())
                {
                Iterator it=list.iterator();
                while(it.hasNext())
                {
                SubFeeHeadBean sbh=(SubFeeHeadBean)it.next();
                stb.append(sbh.getSubFeeTypeId()+"/");
                amount=Fees.Query.getAmount(sbh.getSubFeeTypeId()+"/"+coid+"/"+semester+"/"+catid+"/"+sess);
                if(sbh.getDuration()==1)
                {
                   duration="Once in a Session"; 
                }
                else if(sbh.getDuration()==2)
                {
                    duration ="Annually";
                }
                else if(sbh.getDuration()==3)
                {
                    duration="Half Yearly";
                }
                else if(sbh.getDuration()==4)
                {
                    duration="Quaterly";
                }
                else
                {
                    duration="Monthly";
                }
                for(int k=0;k<list1.size();k++)
                  {
                 StudentFeeDetailsBean ss=(StudentFeeDetailsBean)list1.get(k);
                 if(ss.getSubFeeTypeId()==sbh.getSubFeeTypeId())
                 {
                out.println("<tr><td><center><input type='checkbox' name='"+sbh.getSubFeeTypeId()+"-c' id='"+sbh.getSubFeeTypeId()+"-c' onclick='calculateFees(this.id)' checked></center></td><td>"+sbh.getSubFeeType()+"</td><td style='width:100px'>"+duration+"</td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-a' id='"+sbh.getSubFeeTypeId()+"-a' readonly style='width:150px' value='"+ss.getAmount()+"'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-add' id='"+sbh.getSubFeeTypeId()+"-add' style='width:150px' value='"+ss.getAdditionalAmount()+"' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-r' id='"+sbh.getSubFeeTypeId()+"-r' value='"+ss.getConcessionAmount()+"' style='width:150px' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-p' id='"+sbh.getSubFeeTypeId()+"-p' value='"+ss.getTotalAmount()+"' style='width:150px' readonly></center></td>");
                out.println("</tr>");
                totalamount+=ss.getTotalAmount();
                flag=1;
                break;
                 }
                 else
                 {
                     flag=0;
                 }
                  }
                   if(flag==0)
                   {
                out.println("<tr><td><center><input type='checkbox' name='"+sbh.getSubFeeTypeId()+"-c' id='"+sbh.getSubFeeTypeId()+"-c' onclick='calculateFees(this.id)'></center></td><td>"+sbh.getSubFeeType()+"</td><td style='width:100px'>"+duration+"</td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-a' id='"+sbh.getSubFeeTypeId()+"-a' readonly style='width:150px' value='"+amount+"'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-add' id='"+sbh.getSubFeeTypeId()+"-add' style='width:150px' value='0' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-r' id='"+sbh.getSubFeeTypeId()+"-r' value='0' style='width:150px' onkeyup='checkValue(this.id,this.value)' onblur='recalculate(this.id)' onfocus='calculate(this.id)'></center></td>");
                out.println("<td><center><input type='text' name='"+sbh.getSubFeeTypeId()+"-p' id='"+sbh.getSubFeeTypeId()+"-p' value='0' style='width:150px' readonly></center></td>");
                out.println("</tr>");
                   } 
                }
                 }
                }
                out.println("<tr>");
                out.println("<td colspan='6'><center><h4>Total</h4></center></td>");
                out.println("<td><center><input type='text' name='total' id='total' value='"+totalamount+"' style='width:150px' readonly></center></td>");
                out.println("</tr>");
                out.println("<tr><td colspan='7'><input type='hidden' name='hfeeid' id='hfeeid' value='"+stb.toString()+"'></td></tr>");
                out.println("</tbody></table>");
                out.println("<legend>");
                out.println("Payment Details");
                out.println("</legend>");
                out.println("<div id='lside' style='float: left; margin-top: 10px;'>");
                out.println("<div class='control-group'>");
                out.println("<label for='paid' class='control-label'>Amount Paid(till Date)</label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='paid' id='paid' value='"+fdeposited+"' readonly>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='pamnt' class='control-label'>Payment Amount<span style='color:red'>*</span></label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='pamnt' id='pamnt' class='number required'>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='pmode' class='control-label'>Payment Mode<span style='color:red'>*</span></label>");
                out.println("<div class='controls'>");
                out.println("<select name='pmode' id='pmode' class='{required:true}' onchange='enableButton(this.value)'>");
                out.println("<option value=''>Select Payment Mode</option>");
                out.println("<option value='0'>Cash</option>");
                out.println("<option value='1'>Cheque</option>");
                out.println("<option value='2'>DD</option>");
                out.println("</select>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='bank' class='control-label'>Bank Name</label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='bank' id='bank'>");
                out.println("</div></div>");
                out.println("</div>");
                out.println("<div id='rside' style='float: left; margin-top: 10px'>");
                out.println("<div class='control-group'>");
                out.println("<label for='bamnt' class='control-label'>Balance Amount</label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='bamnt' id='bamnt' value='"+(totalamount-fdeposited)+"' readonly>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='challan' class='control-label'>Challan No.<span style='color:red'>*</span></label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='challan' id='challan' class='required'>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='cheque' class='control-label'>Cheque No.</label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='cheque' id='cheque'>");
                out.println("</div></div>");
                out.println("<div class='control-group'>");
                out.println("<label for='branch' class='control-label'>Branch Name</label>");
                out.println("<div class='controls'>");
                out.println("<input type='text' name='branch' id='branch'>");
                out.println("</div></div>");
                out.println("</div>");
            }
            else
            {
                out.println("<legend>");
                out.println("<p style='color:red'>Please Select Fee Type</p>");
                out.println("</legend>");
            }
        }
            else
            {
                out.println("<legend>");
                out.println("<p style='color:red'>Please Set Fees For This Session/Course</p>");
                out.println("</legend>");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(FeeDetail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(FeeDetail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
